package com.zaicheng.cloud.core.type;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.StandardClassMetadata;
import org.springframework.core.type.StandardMethodMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;

public class StandardAnnotationMetadata extends StandardClassMetadata implements AnnotationMetadata {
    private final Annotation[] annotations;
    private final boolean nestedAnnotationsAsMap;

    public StandardAnnotationMetadata(Class<?> introspectedClass) {
        this(introspectedClass, false);
    }

    public StandardAnnotationMetadata(Class<?> introspectedClass, boolean nestedAnnotationsAsMap) {
        super(introspectedClass);
        this.annotations = introspectedClass.getAnnotations();
        this.nestedAnnotationsAsMap = nestedAnnotationsAsMap;
    }

    @Override
    public Set<String> getAnnotationTypes() {
        Set<String> types = new LinkedHashSet();
        Annotation[] var2 = this.annotations;
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            Annotation ann = var2[var4];
            types.add(ann.annotationType().getName());
        }

        return types;
    }

    @Override
    public Set<String> getMetaAnnotationTypes(String annotationName) {
        return this.annotations.length > 0 ? AnnotatedElementUtils.getMetaAnnotationTypes(this.getIntrospectedClass(), annotationName) : Collections.emptySet();
    }

    @Override
    public boolean hasAnnotation(String annotationName) {
        Annotation[] var2 = this.annotations;
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            Annotation ann = var2[var4];
            if (ann.annotationType().getName().equals(annotationName)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean hasMetaAnnotation(String annotationName) {
        return this.annotations.length > 0 && AnnotatedElementUtils.hasMetaAnnotationTypes(this.getIntrospectedClass(), annotationName);
    }

    @Override
    public MergedAnnotations getAnnotations() {
        return null;
    }

    @Override
    public boolean isAnnotated(String annotationName) {
        return this.annotations.length > 0 && AnnotatedElementUtils.isAnnotated(this.getIntrospectedClass(), annotationName);
    }

    @Override
    public Map<String, Object> getAnnotationAttributes(String annotationName) {
        return this.getAnnotationAttributes(annotationName, false);
    }

    @Override
    @Nullable
    public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
        return this.annotations.length > 0 ? AnnotatedElementUtils.getMergedAnnotationAttributes(this.getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null;
    }

    @Override
    @Nullable
    public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
        return this.getAllAnnotationAttributes(annotationName, false);
    }

    @Override
    @Nullable
    public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
        return this.annotations.length > 0 ? AnnotatedElementUtils.getAllAnnotationAttributes(this.getIntrospectedClass(), annotationName, classValuesAsString, this.nestedAnnotationsAsMap) : null;
    }

    @Override
    public boolean hasAnnotatedMethods(String annotationName) {
        try {
            Method[] methods = this.getIntrospectedClass().getDeclaredMethods();
            Method[] var3 = methods;
            int var4 = methods.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Method method = var3[var5];
                if (!method.isBridge() && method.getAnnotations().length > 0 && AnnotatedElementUtils.isAnnotated(method, annotationName)) {
                    return true;
                }
            }

            return false;
        } catch (Throwable var7) {
            throw new IllegalStateException("Failed to introspect annotated methods on " + this.getIntrospectedClass(), var7);
        }
    }

    @Override
    public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
        try {
            Method[] methods = this.getIntrospectedClass().getDeclaredMethods();
            Set<MethodMetadata> annotatedMethods = new LinkedHashSet(4);
            Method[] var4 = methods;
            int var5 = methods.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Method method = var4[var6];
                if (!method.isBridge() && method.getAnnotations().length > 0 && AnnotatedElementUtils.isAnnotated(method, annotationName)) {
                    annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap));
                }
            }

            return annotatedMethods;
        } catch (Throwable var8) {
            throw new IllegalStateException("Failed to introspect annotated methods on " + this.getIntrospectedClass(), var8);
        }
    }


    static AnnotationMetadata from(Class<?> introspectedClass) {
        return new StandardAnnotationMetadata(introspectedClass, true);
    }

}
